// SetProperties.cpp : implementation file
//

#include "stdafx.h"
#include "shlobj.h"
#include <string>
#include "Virtual Pole.h"
#include "SetProperties.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetProperties dialog


CSetProperties::CSetProperties(CWnd* pParent /*=NULL*/)
	: CDialog(CSetProperties::IDD, pParent)
	, m_lFrameExposure(0)
	, m_csCameraMode(_T(""))
	, m_csCameraControlLine(_T(""))
	, m_csCameraTriggerLine(_T(""))
	, m_csCameraTriggerOutLine(_T(""))
{
	//{{AFX_DATA_INIT(CSetProperties)
	m_bVideoLength = FALSE;
	//}}AFX_DATA_INIT
}


void CSetProperties::DoDataExchange(CDataExchange* pDX) {
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetProperties)
	DDX_Text(pDX, IDC_EDIT_Path, m_csLogFilePath);
	DDV_MaxChars(pDX, m_csLogFilePath, 240);
	DDX_Text(pDX, IDC_EDIT_Video_Path, m_csVideoFilePath);
	DDV_MaxChars(pDX, m_csVideoFilePath, 240);
	DDX_Text(pDX, IDC_EDIT_Settings_Path, m_csSettingsPath);
	DDV_MaxChars(pDX, m_csSettingsPath, 240);
	DDX_Check(pDX, IDC_Log_Check, m_bSaveLog);
	DDX_Text(pDX, IDC_VIDEO_FPS_EDIT, m_iFPS);
	DDV_MinMaxUInt(pDX, m_iFPS, 1, 1500);
	DDX_Text(pDX, IDC_VIDEO_BUFSIZE_EDIT, m_iBufSize);
	DDV_MinMaxUInt(pDX, m_iBufSize, 1, 3072);
	DDX_Check(pDX, IDC_Video_Length_Check, m_bVideoLength);
	DDX_Text(pDX, IDC_VIDEO_LENGHTH_EDIT, m_lMaxVideoLength);
	DDV_MinMaxLong(pDX, m_lMaxVideoLength, 1, 2145500000);
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_VIDEO_EXPOSURE_EDIT, m_lFrameExposure);
	DDX_CBString(pDX, IDC_CAMERA_MODE_COMBO, m_csCameraMode);
	DDX_CBString(pDX, IDC_CAMERA_EXSYNC_COMBO, m_csCameraControlLine);
	DDX_CBString(pDX, IDC_CAMERA_TRIGGER_COMBO, m_csCameraTriggerLine);
	DDX_Control(pDX, IDC_SPIN1, m_spinExposure);
	DDX_CBString(pDX, IDC_CAMERA_TRIGGER_OUT_COMBO, m_csCameraTriggerOutLine);
}


BEGIN_MESSAGE_MAP(CSetProperties, CDialog)
	//{{AFX_MSG_MAP(CSetProperties)
	ON_BN_CLICKED(IDC_Log_Check, OnLogCheck)
	ON_BN_CLICKED(IDC_LOG_Browse, OnLOGBrowse)
	ON_BN_CLICKED(IDC_Video_Browse, OnVideoBrowse)
	ON_BN_CLICKED(IDC_BTN_SAVE_SETTINGS, OnBtnSaveSettings)
	ON_BN_CLICKED(IDC_Settings_Browse, OnSettingsBrowse)
	ON_BN_CLICKED(IDC_BTN_Load_SETTINGS, OnBTNLoadSETTINGS)
	ON_BN_CLICKED(IDC_Video_Length_Check, OnVideoLengthCheck)
	//}}AFX_MSG_MAP
	ON_EN_KILLFOCUS(IDC_VIDEO_EXPOSURE_EDIT, &CSetProperties::OnEnKillfocusVideoExposureEdit)
	ON_EN_KILLFOCUS(IDC_VIDEO_FPS_EDIT, &CSetProperties::OnEnKillfocusVideoFpsEdit)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetProperties message handlers

void CSetProperties::OnLogCheck() {
	// TODO: Add your control notification handler code here
	m_bSaveLog = !m_bSaveLog;
	GetDlgItem(IDC_EDIT_Path) ->EnableWindow(m_bSaveLog);
	GetDlgItem(IDC_LOG_Browse) ->EnableWindow(m_bSaveLog);
}


bool GetFolder(std::string& folderpath, const char* szCaption = NULL, HWND hOwner = NULL) {
	bool retVal = false;

	// The BROWSEINFO struct tells the shell 
	// how it should display the dialog.
	BROWSEINFO bi;
	memset(&bi, 0, sizeof(bi));

	bi.ulFlags   = BIF_EDITBOX ;
	bi.hwndOwner = hOwner;
	bi.lpszTitle = szCaption;

	// must call this if using BIF_USENEWUI
	::OleInitialize(NULL);

	// Show the dialog and get the itemIDList for the selected folder.
	LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);

	if(pIDL != NULL) {
		// Create a buffer to store the path, then get the path.
		char buffer[_MAX_PATH] = {'\0'};
		if(::SHGetPathFromIDList(pIDL, buffer) != 0)
		{
			// Set the string value.
			folderpath = buffer;
			retVal = true;
		}		

		// free the item id list
		CoTaskMemFree(pIDL);
	}
	::OleUninitialize();
	return retVal;
}

void CSetProperties::OnLOGBrowse() {
	std::string szPath("");
	if (GetFolder(szPath, "Select a folder.") == true) {
		m_csLogFilePath = CString(szPath.c_str());
		CString LastChar = m_csLogFilePath.GetAt(m_csLogFilePath.GetLength()-1);
		if (LastChar.Compare("\\") != 0)
			m_csLogFilePath+= "\\";
		UpdateData(false);
	}
}

// Function to execute when user browser for the folder to save videos in
// Initiated from Preferences dialog window
void CSetProperties::OnVideoBrowse() 
{
	std::string szPath("");
	if (GetFolder(szPath, "Select a folder.") == true)
	{
		m_csVideoFilePath = CString(szPath.c_str());
		CString LastChar = m_csVideoFilePath.GetAt(m_csVideoFilePath.GetLength()-1);
		if (LastChar.Compare("\\") != 0)
			m_csVideoFilePath+= "\\";
		UpdateData(false);
	}
}

void CSetProperties::OnBtnSaveSettings() 
{
	// Create the settings file if user wants to save settings
	CTime now = CTime::GetCurrentTime();
	
	OnSettingsBrowse();
	
	CString s = CString(m_csSettingsPath + "VPSettings " + now.Format("%H_%M_%d_%m_%y") + ".txt");

	TRY	{
		m_SettingsFile.Open(s, CFile::modeCreate | CFile::modeWrite | CFile::typeText);
	}
	CATCH( CFileException, e )
	{
		#ifdef _DEBUG
			afxDump << "File could not be opened " << e->m_cause << "\n";
		#endif
	}
	END_CATCH

	// write the integer settings to the file
	s.Format("%.3u   - The number of the selected pole from the pols list [#]\n",m_SaveData.intSettings[0]+1); // we add 1 to avoid negative integers
	m_SettingsFile.WriteString(s);
	s.Format("%.3u   - Total number poles on the pole list [#]\n",m_SaveData.intSettings[1]);
	m_SettingsFile.WriteString(s);
	s.Format("%.4u  - ROI width [pix]\n",m_SaveData.intSettings[2]);
	m_SettingsFile.WriteString(s);
	s.Format("%.4u  - ROI height [pix]\n",m_SaveData.intSettings[3]);
	m_SettingsFile.WriteString(s);
	s.Format("%.4u  - ROI X position [pix]\n",m_SaveData.intSettings[4]);
	m_SettingsFile.WriteString(s);
	s.Format("%.4u  - ROI Y position [pix]\n",m_SaveData.intSettings[5]);
	m_SettingsFile.WriteString(s);
	s.Format("%.3u   - Motion threshold [gray level]\n",m_SaveData.intSettings[6]);
	m_SettingsFile.WriteString(s);
	s.Format("%.3u   - Pole threshold [%%]\n",m_SaveData.intSettings[7]);
	m_SettingsFile.WriteString(s);
	s.Format("%.3u   - Median threshold [#]\n",m_SaveData.intSettings[8]);
	m_SettingsFile.WriteString(s);
	s.Format("%.4u  - FPS [#/sec]\n",m_SaveData.intSettings[9]);
	m_SettingsFile.WriteString(s);
	s.Format("%.4u  - Video buffer size [MB]\n",m_SaveData.intSettings[10]);
	m_SettingsFile.WriteString(s);
	s.Format("%.5u - Video buffer size [# Frames]\n",m_SaveData.intSettings[11]);
	m_SettingsFile.WriteString(s);
	s.Format("%.5u   - Frame exposure [usec]\n",m_SaveData.intSettings[15]);
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString("\n");

	// write the boolean settings to the file
	s.Format("%.1u - Mesurement units are mm\n",m_SaveData.boolSettings[0]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - Select pole by external program\n",m_SaveData.boolSettings[1]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - Save log file\n",m_SaveData.boolSettings[2]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - Don't Analyze Pole Position\n",m_SaveData.boolSettings[3]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View the tool bar\n",m_SaveData.boolSettings[4]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View the status bar\n",m_SaveData.boolSettings[5]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View only the ROI\n",m_SaveData.boolSettings[6]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View the pole location\n",m_SaveData.boolSettings[7]);
	m_SettingsFile.WriteString(s);	
	s.Format("%.1u - View analyzed video\n",m_SaveData.boolSettings[8]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View captured video\n",m_SaveData.boolSettings[9]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View frame numbers\n",m_SaveData.boolSettings[10]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View retraction / protraction state\n",m_SaveData.boolSettings[11]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - Keep aspect ratio\n",m_SaveData.boolSettings[12]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - View trigger squares\n",m_SaveData.boolSettings[14]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - Detect motion only inside virtual objects\n",m_SaveData.boolSettings[19]);
	m_SettingsFile.WriteString(s);
	s.Format("%.1u - Enable motion detection\n", m_SaveData.boolSettings[20]);
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString("\n");

	//write the path settings to the file
	s = "- Logfile Path\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[0]+"\n");
	s ="- Videofile Path\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[1]+"\n"); 
	s ="- Settings File Path\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[2]+"\n");
	s ="- Camera Mode\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[3]+"\n");
	s ="- Camera Control Line\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[4]+"\n");
	s ="- Camera Trigger Line\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[5]+"\n");
	s ="- Camera Trigger Out Line\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[6]+"\n");
	s ="- Tracking output channels \n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[7]+"\n");
	s ="- Event trigger channel\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[9]+"\n");
	s ="- Event trigger device\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[10]+"\n");
	s ="- Indicator trigger input channel\n";
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString(m_SaveData.Paths[11]+"\n");
	m_SettingsFile.WriteString("\n");

	// write the conversation factors to the file
	s.Format("X: %.6f Y: %.6f - A conversation factors to convert pixels to mm in both directions  [Pix/MM]\n",m_SaveData.ConversionFactor.x,m_SaveData.ConversionFactor.y);
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString("\n");

	// write the angle of the medial line to the file
	s.Format("%.6f - The angle of the medial line [degrees]\n",m_SaveData.MedLineAngle);
	m_SettingsFile.WriteString(s);
	m_SettingsFile.WriteString("\n");

	// write the pole list to the file only if the list is not empty
	if (m_SaveData.intSettings[1]>0)
	{
		s="X Position[pix] Y Position[pix] Width[pix] Height[pix]\n";
		m_SettingsFile.WriteString(s);
		for (int i=0;i<m_SaveData.intSettings[1];i++)
			m_SettingsFile.WriteString(*(m_SaveData.PoleList+i)+"\n");
	}
	m_SettingsFile.Close();
	return;
}

void CSetProperties::OnSettingsBrowse() 
{
	// TODO: Add your control notification handler code here
	std::string szPath("");

	if (GetFolder(szPath, "Select a folder.") == true)
	{
		m_csSettingsPath = CString(szPath.c_str());
		CString LastChar = m_csSettingsPath.GetAt(m_csSettingsPath.GetLength()-1);
		if (LastChar.Compare("\\") != 0)
			m_csSettingsPath+= "\\";
		UpdateData(false);
	}
}	

void CSetProperties::OnBTNLoadSETTINGS()  {
	static char BASED_CODE szFilter[] = "VirtualPole Settings Files (*.txt)|*.txt|";

	CFileDialog fdFile(true,NULL,NULL,OFN_HIDEREADONLY , szFilter);
	if (fdFile.DoModal()!=IDOK)
		return;
	//AfxMessageBox(LPCTSTR(fdFile.GetPathName()), MB_ICONINFORMATION | MB_OK);
	CString s = fdFile.GetPathName();

	TRY {
		m_SettingsFile.Open(s, CFile::modeRead | CFile::typeText);
	} CATCH( CFileException, e ) {
		#ifdef _DEBUG
			afxDump << "File could not be opened " << e->m_cause << "\n";
		#endif
	}
	END_CATCH
	
	// read the integer settings from the file
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[0] = atol(s.Mid(0,3)) -1; // we subtract 1 to to negate the addition of 1 in the saving processs
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[1] = atol(s.Mid(0,3));
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[2] = atol(s.Mid(0,4)); // ROI
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[3] = atol(s.Mid(0,4)); // ROI
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[4] = atol(s.Mid(0,4)); // ROI
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[5] = atol(s.Mid(0,4)); // ROI
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[6] = atol(s.Mid(0,3));
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[7] = atol(s.Mid(0,3));
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[8] = atol(s.Mid(0,3));
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[9] = atol(s.Mid(0,4));
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[10] = atol(s.Mid(0,4));
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[11] = atol(s.Mid(0,5));
	m_SettingsFile.ReadString(s);
	m_SaveData.intSettings[15] = atol(s.Mid(0,5));
	m_SettingsFile.ReadString(s);
	
	// read the boolean settings from the file
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[0] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[1] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[2] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[3] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[4] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[5] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[6] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[7] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[8] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[9] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[10] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[11] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[12] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[14] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[15] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[16] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[17] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[18] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	m_SaveData.boolSettings[19] = (bool)atoi(s.Mid(0,1));
	m_SettingsFile.ReadString(s);
	
	// read the path settings from the file
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[0]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[1]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[2]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[3]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[4]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[5]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[6]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[7]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[8]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[9]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[10]);
	m_SettingsFile.ReadString(s);
	m_SettingsFile.ReadString(m_SaveData.Paths[11]);

	// read the conversation factors from the file
	m_SettingsFile.ReadString(s);
	m_SaveData.ConversionFactor.x = atoi(s.Mid(3,10));
	m_SaveData.ConversionFactor.y = atoi(s.Mid(15,22));
	
	// read the angle of the medial line from the file 
	m_SettingsFile.ReadString(s);
	m_SaveData.MedLineAngle = atoi(s.Mid(0,6));

	m_SettingsFile.ReadString(s);

	// read the pole list from the file but only if the list is not empty
	if (m_SaveData.PoleList) {
		delete [] m_SaveData.PoleList;
		m_SaveData.PoleList = NULL;
	}

	if (m_SaveData.intSettings[1]>0) {
		m_SaveData.PoleList = new CString[m_SaveData.intSettings[1]];
		m_SettingsFile.ReadString(s);
		for (int i=0;i<m_SaveData.intSettings[1];i++)
			m_SettingsFile.ReadString(*(m_SaveData.PoleList+i));
	}
	m_SettingsFile.Close();

	// update the general setting dialog
	m_csLogFilePath		= m_SaveData.Paths[0];
	m_csVideoFilePath	= m_SaveData.Paths[1];
	m_csSettingsPath	= m_SaveData.Paths[2];
	m_csCameraMode		= m_SaveData.Paths[3];
	m_csCameraControlLine		= m_SaveData.Paths[4];
	m_csCameraTriggerLine		= m_SaveData.Paths[5];
	m_csCameraTriggerOutLine	= m_SaveData.Paths[6];

	m_bSaveLog			= m_SaveData.boolSettings[2];
	m_iFPS				= m_SaveData.intSettings[9];
	m_iBufSize			= m_SaveData.intSettings[10];
	m_lMaxVideoLength	= m_SaveData.intSettings[11];
	m_lFrameExposure	= m_SaveData.intSettings[15];

	// update the ROI dialog
	
	m_dSetROI.m_lROI_Width	= m_SaveData.intSettings[2];
	m_dSetROI.m_lROI_Height = m_SaveData.intSettings[3];
	m_dSetROI.m_lROI_Left	= m_SaveData.intSettings[4];
	m_dSetROI.m_lROI_Bottom = m_SaveData.intSettings[5];

	UpdateData(false);
	m_bSaveLog = !m_bSaveLog;
	OnLogCheck();
}


void CSetProperties::OnOK() {
	UpdateData(true);
	if (m_SaveData.intSettings[9]!= m_iFPS 
		|| m_SaveData.intSettings[11] != m_lMaxVideoLength 
		|| m_SaveData.intSettings[10] != m_iBufSize)
		AfxMessageBox("Changes in frame rate or buffer size will take affect only after stopping the acqusition", MB_ICONINFORMATION | MB_OK);
	m_SaveData.Paths[0]			= m_csLogFilePath;
	m_SaveData.Paths[1]			= m_csVideoFilePath;
	m_SaveData.Paths[2]			= m_csSettingsPath;
	m_SaveData.Paths[3]			= m_csCameraMode;	
	m_SaveData.Paths[4]			= m_csCameraControlLine;	
	m_SaveData.Paths[5]			= m_csCameraTriggerLine;
	m_SaveData.Paths[6]			= m_csCameraTriggerOutLine;

	m_SaveData.boolSettings[2]	= m_bSaveLog;
	m_SaveData.intSettings[9]	= m_iFPS;
	m_SaveData.intSettings[10]	= m_iBufSize;
	m_SaveData.intSettings[11]	= m_lMaxVideoLength;
	m_SaveData.intSettings[15]	= m_lFrameExposure;
	CDialog::OnOK();
}

void CSetProperties::OnVideoLengthCheck() {
	m_bVideoLength = !m_bVideoLength;
	GetDlgItem(IDC_STATIC_Video_Size) ->EnableWindow(m_bVideoLength);
	GetDlgItem(IDC_VIDEO_LENGHTH_EDIT) ->EnableWindow(m_bVideoLength);
}

BOOL CSetProperties::OnInitDialog() {
	CDialog::OnInitDialog();
	
	m_spinExposure.SetDecimalPlaces (0);
	m_spinExposure.SetTrimTrailingZeros (true);
	m_spinExposure.SetRangeAndDelta (1, 1000000, 1);
	m_spinExposure.SetPos (m_lFrameExposure);

	GetDlgItem(IDC_STATIC_Video_Size) ->EnableWindow(m_bVideoLength);
	GetDlgItem(IDC_VIDEO_LENGHTH_EDIT) ->EnableWindow(m_bVideoLength);
	GetDlgItem(IDC_EDIT_Path) ->EnableWindow(m_bSaveLog);
	GetDlgItem(IDC_LOG_Browse) ->EnableWindow(m_bSaveLog);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// Check that exposure time is within range [0 1/frames/sec]
void CSetProperties::OnEnKillfocusVideoExposureEdit() {
	UpdateData(true);
	long lMaxExposure = (1000000 / m_iFPS) - 5;
	if (m_lFrameExposure < 1) {
		AfxMessageBox("Exposure time cannot be smaller than 1");
		m_lFrameExposure = 1;
	} else if (m_lFrameExposure > lMaxExposure) {
		CString csWarn;
		csWarn.Format("Exposure time cannot be larger than %d", lMaxExposure);
		AfxMessageBox(csWarn);
		m_lFrameExposure = lMaxExposure;
	}
	UpdateData(false);		
	UpdateWindow();
}

/*	Make sure framerate and exposure time is consistent. If not, then adjust
	exposure time to max permitted.
*/
void CSetProperties::OnEnKillfocusVideoFpsEdit() {
	OnEnKillfocusVideoExposureEdit();
}
